﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Globalization;
using Pantry.Entity;
using Pantry.Commons;
using Pantry.Business;

namespace Pantry.Web.Users
{
    public partial class CreateContactForm : System.Web.UI.Page
    {
        private ContactBus _contactBus;
        private Contact _contact;
        private DataView _dataView;
        private static bool _isSort = true;
        private readonly IFormatProvider _formatProvider = CultureInfo.CurrentCulture;
        protected void Page_Load(object sender, EventArgs e)
        {
            FilterContact(TextBoxFirstNameSearch.Text, TextBoxSurnameSearch.Text, CheckBoxIncludeInactive.Checked);
            BindToListContactType();
            BindToListContactMethod();
        }
        #region Method

        private void FilterContact(string firstName, string surName, bool isIncludeInactiveContact)
        {
            try
            {
                _contactBus = new ContactBus();
                DataTable dataTable = _contactBus.ListContact();
                _dataView = dataTable.DefaultView;
                string statement = string.Format(_formatProvider, "FullName like '%{0}%' AND FullName LIKE '%{1}%'", firstName, surName);
                if (!isIncludeInactiveContact)
                {
                    statement += "AND IsActive = 'true'";
                }
                _dataView.RowFilter = statement;
                CollectionPager1.PageSize = 5;
                CollectionPager1.BindToControl = RepeaterContact;
                CollectionPager1.DataSource = _dataView;
                RepeaterContact.DataSource = CollectionPager1.DataSourcePaged;
                RepeaterContact.DataBind();
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        private void SortContact(string field)
        {
            try
            {
                if (_isSort)
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} DESC", field);
                    _isSort = false;
                }
                else
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} ASC", field);
                    _isSort = true;
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        private void BindToListContactType()
        {
            _contactBus = new ContactBus();
            DataTable dataTable = _contactBus.ListContactType();
            ListContactType.DataSource = dataTable.DefaultView;
            ListContactType.DataValueField = "ID";
            ListContactType.DataTextField = "ContactType";
            ListContactType.DataBind();
        }

        private void BindToListContactMethod()
        {
            _contactBus = new ContactBus();
            DataTable dataTable = _contactBus.ListBestContactMethod();
            ListContactMethod.DataSource = dataTable.DefaultView;
            ListContactMethod.DataValueField = "ID";
            ListContactMethod.DataTextField = "BestContactMethod";
            ListContactMethod.DataBind();
        }
        #endregion
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            _contactBus = new ContactBus();
            _contact = new Contact
            {
                ContactId = 1,
                FirstName = TextBoxFirstName.Text,
                Surname = TextBoxSurname.Text,
                KnownAs = TextBoxKnownAs.Text,
                OfficePhone = TextBoxOfficePhone.Text,
                MobilePhone = TextBoxMobilePhone.Text,
                HomePhone = TextBoxHomePhone.Text,
                Email = TextBoxEmail.Text,
                ManagerId = int.Parse(HiddenManagerId.Value, _formatProvider),
                ContactType = int.Parse(ListContactType.SelectedValue, _formatProvider),
                BestContactMethod = int.Parse(ListContactMethod.SelectedValue, _formatProvider),
                JobRole = TextBoxJobRole.Text,
                WorkBase = TextBoxWorkBase.Text,
                JobTitle = TextBoxJobTitle.Text,
                IsActive = CheckBoxIsActive.Checked
            };
            bool checkUserExists = _contactBus.CheckUserIsExists(_contact.MobilePhone, _contact.Email);
            if (!checkUserExists)
            {
                bool result = _contactBus.CreateNewContact(_contact);
                UpdateLabel.Text = result ? "This contact has been created successfully!" : "There is a error!";
            }
            else
            {
                UpdateLabel.Text = "You have to check moblie phone and email field. They is conflict with a exists contact. ";
            }
        }
        protected void ButtonLookupClick(object sender, EventArgs e)
        {
            PanelPopup.Visible = true;
            PanelPopup.CssClass = "popup-container";
            FilterContact(TextBoxFirstNameSearch.Text, TextBoxSurnameSearch.Text, CheckBoxIncludeInactive.Checked);
        }
        protected void ButtonBackClick(object sender, EventArgs e)
        {
            Response.Redirect("~/Users/ViewListContact.aspx");
        }
        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            FilterContact(TextBoxFirstNameSearch.Text, TextBoxSurnameSearch.Text, CheckBoxIncludeInactive.Checked);
        }
        protected void CheckBoxIncludeActiveCheckedChanged(object sender, EventArgs e)
        {
            FilterContact(TextBoxFirstNameSearch.Text, TextBoxSurnameSearch.Text, CheckBoxIncludeInactive.Checked);
        }
        protected void ButtonSelectClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(HttpHelper.GetFormString("RadioContactId")))
            {
                string[] contact = HttpHelper.GetFormString("RadioContactId").Split('$');
                TextBoxManagerName.Text = contact[1];
                HiddenManagerId.Value = contact[0];
                PanelPopup.Visible = false;
            }
            else
            {
                PanelPopup.Visible = false;
            }
        }
        protected void ButtonCloseClick(object sender, EventArgs e)
        {
            PanelPopup.Visible = false;
        }
        protected void ButtonSortByNameClick(object sender, EventArgs e)
        {
            SortContact("FullName");
        }
        protected void ButtonSortByMobileClick(object sender, EventArgs e)
        {
            SortContact("MobilePhone");
        }
        protected void ButtonSortByEmailClick(object sender, EventArgs e)
        {
            SortContact("Email");
        }
        protected void ButtonSortByStatusClick(object sender, EventArgs e)
        {
            SortContact("IsActive");
        }
        protected void ButtonSortByContactTypeClick(object sender, EventArgs e)
        {
            SortContact("ContactType");
        }

    }
}
